package com.DHH.Service.Base.Impl;
import com.DHH.Config.ApiConstant;
import com.DHH.Entity.Base.BaseReUserRole;
import com.DHH.Entity.Base.BaseRole;
import com.DHH.Entity.Base.BaseUser;
import com.DHH.Entity.Base.BaseUserInfo;
import com.DHH.Mapper.Base.BaseReUserRoleMapper;
import com.DHH.Mapper.Base.BaseRoleMapper;
import com.DHH.Mapper.Base.BaseUserInfoMapper;
import com.DHH.Mapper.Base.BaseUserMapper;
import com.DHH.Service.Base.IBaseUserService;
import com.DHH.Util.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BaseUserServiceImpl implements IBaseUserService{

    @Autowired
    private BaseUserMapper baseUserMapper;
    @Autowired
    private BaseRoleMapper baseRoleMapper;
    @Autowired
    private BaseReUserRoleMapper baseReUserRoleMapper;
    @Autowired
    private BaseUserInfoMapper baseUserInfoMapper;

    /**
     * 普通情况的用户注册
     * @param user
     * @return
     */
    @Override
    public BaseUser insertBaseUser(BaseUser user){
        user.setRegisterTime(new Date());
        String salt= MyUtil.getRandomCharAndNumr(12);
        user.setSalt(salt);
        String password= MyUtil.createMD5(user.getToken());//对密码进行第一次加密
        user.setToken(MyUtil.createMD5(password+salt));//对密码进行加盐并二次加密
        BaseRole role=new BaseRole();
        role.setIsDefault(1);
        List<BaseRole> roleList=baseRoleMapper.select(role);//获取默认的角色
        baseUserMapper.insertSelective(user);
        Integer userId=user.getId();
        for(BaseRole r:roleList)
        {
            BaseReUserRole reUserRole=new BaseReUserRole();
            reUserRole.setRoleId(r.getId());
            reUserRole.setUserId(userId);
            baseReUserRoleMapper.insert(reUserRole);
            //给用户添加默认角色
        }
        BaseUserInfo baseUserInfo =null;
        if(user.getBaseUserInfo()!=null)
        {
            baseUserInfo=user.getBaseUserInfo();
        }else
        {
            baseUserInfo=new BaseUserInfo();
            baseUserInfo.setUserId(userId);
        }

        baseUserInfo.setSurplusMoney(ApiConstant.wx_USER_INITMONEY);
        baseUserInfo.setSecurityMoney((float)0.0);
        baseUserInfoMapper.insert(baseUserInfo);
        user.setBaseUserInfo(baseUserInfo);
        return  user;
    }

    /**
     * 后台添加用户
     * @param user
     * @return
     */
    public BaseUser insertBaseUserAdmin(BaseUser user, List<BaseRole> roleList){
        user.setRegisterTime(new Date());
        user.setUserSource("后台添加");
        String salt= MyUtil.getRandomCharAndNumr(12);
        user.setSalt(salt);
        String password= MyUtil.createMD5(user.getToken());//对密码进行第一次加密
        user.setToken(MyUtil.createMD5(password+salt));//对密码进行加盐并二次加密
        BaseRole role=new BaseRole();
        role.setIsDefault(1);

        baseUserMapper.insertSelective(user);
        Integer userId=user.getId();
        for(BaseRole r:roleList)
        {
            BaseReUserRole reUserRole=new BaseReUserRole();
            reUserRole.setRoleId(r.getId());
            reUserRole.setUserId(userId);
            baseReUserRoleMapper.insert(reUserRole);
        }
        BaseUserInfo baseUserInfo =null;
        if(user.getBaseUserInfo()!=null)
        {
            baseUserInfo=user.getBaseUserInfo();
        }else
        {
            baseUserInfo=new BaseUserInfo();
            baseUserInfo.setUserId(userId);
            baseUserInfo.setSecurityMoney(new Float(0));
            baseUserInfo.setSurplusMoney(new Float(0));
        }
        baseUserInfoMapper.insert(baseUserInfo);
        user.setBaseUserInfo(baseUserInfo);
        return  user;
    }
    @Override
    public BaseUser updateBaseUser(BaseUser baseUser){
        baseUserMapper.updateByPrimaryKeySelective(baseUser);
        return baseUser;
    };
    @Override
    public BaseUser deleteBaseUser(BaseUser baseUser){

        baseUserMapper.delete(baseUser);
        return baseUser;
    };
    /**
     * 保存或更新操作，返回值为主表
     */
    public BaseUser saveOrUpdateBaseUser(BaseUser baseUser,List<BaseRole> roleList){
        // baseUserMapper.updateByPrimaryKeySelective(baseUser);
        BaseReUserRole baseReUserRole=new BaseReUserRole();
        baseReUserRole.setUserId(baseUser.getId());
        baseReUserRoleMapper.delete(baseReUserRole);
        for(BaseRole r:roleList)
        {
            BaseReUserRole reUserRole=new BaseReUserRole();
            reUserRole.setRoleId(r.getId());
            reUserRole.setUserId(baseUser.getId());
            baseReUserRoleMapper.insert(reUserRole);
        }
        return baseUser;
    }
}
